package com.yanqu.road.server.cmd.activity.suitang;

import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.suitang.data.SuiTangUserData;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangStage;
import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_SUI_TANG_MOVE_SPEED_UP, desc = "移动加速")
public class SuiTangMoveSpeedUpCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SuiTangConfigSync config = SuiTangMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (!config.inEnterActivityTime()) {
            ret = GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
            return;
        }
        eSuiTangStage stage = config.getSuiTangStage();
        if (stage != eSuiTangStage.Stage1Time) {
            ret = GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
            return;
        }

        SuiTang.SuiTangMoveSpeedUpReqMsg.Builder reqMsg = SuiTang.SuiTangMoveSpeedUpReqMsg.newBuilder();

        // 需要消耗元宝
        SuiTangUserData userData = player.getModule(SuiTangModule.class).getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SUI_TANG_PARAM_ERR;
            return;
        }

        synchronized (userData) {
            int upNum = (int) userData.getMoveSpeedUpNum() + 1;
            Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(config.getParamConfig().getSuiTangCityMoveBuffCost(upNum)));
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
                ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(property);
            userData.setMoveSpeedUpNum(upNum);
            reqMsg.setTimes(upNum);
        }

        // 跨服执行操作
        SuiTang.CrossSuiTangMoveSpeedUpReqMsg.Builder builder = SuiTang.CrossSuiTangMoveSpeedUpReqMsg.newBuilder();
        builder.setActivityId(config.getActivityInfo().getActivityId());
        builder.setReq(reqMsg);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_SUI_TANG_MOVE_SPEED_UP, builder, config.getActivityInfo().getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }

        SuiTang.SuiTangMoveSpeedUpRespMsg.Builder respMsg = SuiTang.SuiTangMoveSpeedUpRespMsg.newBuilder();
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_SUI_TANG_MOVE_SPEED_UP, respMsg);
    }
}
